package com.bookstore;

import java.util.Calendar;

import com.bookstore.bean.Compra;
import com.bookstore.db.BookStoreDB;
import com.bookstore.db.BookStoreDao;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ListCategoryActivity extends MenuListActivity {
    /** Called when the activity is first created. */
	private BookStoreDao dao;
	private static final int ACTIVITY_BOOK_LIST = 2;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.categories_list);
        if (dao==null){
        	dao = new BookStoreDao(this);
            dao.open();
        }
        loadCategories();
        registerForContextMenu(getListView());
    }    
    
    @Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
	    super.onListItemClick(l, v, position, id);
	    Intent i = new Intent(this, ListBookActivity.class);
	    i.putExtra(BookStoreDB.KEY_ROWID, id);
	    startActivityForResult(i, ACTIVITY_BOOK_LIST);
	}
    
    private void loadCategories() {
		Cursor c = dao.fetchAllCategorias();
		startManagingCursor(c);
		String[] from = new String[] { "title" };
		int[] to = new int[] { R.id.labelCategory };
		SimpleCursorAdapter categories = new SimpleCursorAdapter(this,
				R.layout.categories_row, c, from, to);
		setListAdapter(categories);
	}
}
